<?php

namespace app\home\controller;


use think\facade\App;
use think\facade\Request;

class UrlChannel extends Base {
    
    protected function initialize()
    {
        parent::initialize();
        $this->checkLogin();
    }
    
    /**
     * @desc 我的渠道
     */
    public function index() {
        $params = [
            'startDate'   => input('startDate', '', 'trim'),
            'endDate'     => input('endDate', '', 'trim'),
            'channelName' => input('channelName', '', 'trim'),
            'channelKey'  => input('channelKey', '', 'trim'),
            'channelCode' => input('channelCode', '', 'trim'),
            'status'      => input('status', ''),
            'pageSize'    => input('pageSize', 10)
        ];

        $data = model('UrlChannel', 'logic')->index($params, $this->userId);
        $this->assign($data);
        return view();
    }
    
    /**
     * @desc 添加分组
     */
    public function add() {
        return view();
    }
    
    /**
     * @desc 编辑分组
     */
    public function edit() {
        $channelId = input('id', 0);
        $data = model('UrlChannel', 'logic')->edit($channelId, $this->userId);

        $this->assign($data);
        return view();
    }
    
    /**
     * @desc 创建链接
     */
    public function operateAjax() {
        $params = [
            'id'           => Request::post('id', 0),
            'action'       => Request::post('action', 'add'),
            'paramsSwitch' => Request::post('paramsSwitch', 1),
            'channelName'  => Request::post('channelName'),
            'channelKey'   => Request::post('channelKey', 'channelCode'),
            'channelCode'  => Request::post('channelCode'),
            'status'       => Request::post('status'),
            'remark'       => Request::post('remark'),
        ];
        
        $logic = model('UrlChannel', 'logic');
        $data = [];
        try {
            $data = $logic->operateAjax($params, $this->userId);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode, $logic->message);
    }

    /**
     * @desc 批量添加渠道
     */
    public function batchAdd() {
        $data = model('UrlChannel', 'logic')->batchAdd($this->userId);

        $this->assign($data);
        return view();
    }

    /**
     * @desc 批量生成渠道和短链接
     */
    public function batchAddAjax() {
        $params = [
            'keyword'      => Request::post('keyword', 1),
            'link'         => Request::post('link', 1),
            'paramsSwitch' => Request::post('paramsSwitch', 1),
            'channelNum'   => Request::post('channelNum', 1),
            'channelName'  => Request::post('channelName'),
            'channelKey'   => Request::post('channelKey', 'channelCode'),
            'channelCode'  => Request::post('channelCode'),
            'status'       => Request::post('status'),
            'remark'       => Request::post('remark', '','trim'),
        ];

        $logic = model('UrlChannel', 'logic');
        $data = [];
        try {
            $data = $logic->batchAddAjax($params, $this->userId);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode, $logic->message);
    }
}
